package com.mvc.core.utils.excel;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

/**
 * 
 * excel导出头类文件 ,如果表头合并，只需设置hearderRules，无需设置title,hearder,
 *
 * @author lt
 * @version 2017年11月21日上午9:45:01
 */
public class ExportRules {

	/**
	 * 是否带序号
	 */

	private boolean autoNum;

	/**
	 * 导出字段设置，
	 * 
	 */

	private Object[][] fields;

	/**
	 * 表头名
	 */
	private String titile;

	/**
	 * 标题列
	 */
	private String[] header;

	/**
	 * excel头：合并规则及值，rules.put("1,1,A,G", "其它应扣"); 对应excel位置
	 */
	private HashMap<String, String> headerRules;

	/**
	 * excel尾 ： 合并规则及值，rules.put("1,1,A,G", 值); 对应excel位置
	 */
	private HashMap<String, String> footerRules;

	// --------------------无关设置字段-------------------------

	/**
	 * 最大单元格列数
	 */
	private int maxColumns = 0;

	/**
	 * 表头最大行数
	 */
	private int maxRows = 0;

	/**
	 * 是否合并表头
	 */
	private boolean ifMerge;

	/**
	 * 是否有页脚
	 */
	private boolean ifFooter;

	/**
	 * 常规一行表头构造,不带尾部
	 * 
	 * @param autoNum 是否自动序号
	 * @param fields 对应自动及列宽
	 * @param titile 大标题
	 * @param header 表头标题
	 * @param footerRules 数据尾行合计
	 */
	public ExportRules(boolean autoNum, Object[][] fields, String titile, String[] header, HashMap<String, String> footerRules) {
		super();
		this.autoNum = autoNum;
		this.fields = fields;
		if (titile != null) {
			setTitile(titile);
		}
		setHeader(header);
		if (footerRules != null) {
			setFooterRules(footerRules);
		}
	}

	/**
	 * 复杂表头构造
	 * 
	 * @param autoNum 是否自动序号
	 * @param fields 对于字段及列宽
	 * @param headerRules 表头设计
	 * @param footerRules 尾部合计栏设计
	 */
	public ExportRules(boolean autoNum, Object[][] fields, HashMap<String, String> headerRules, HashMap<String, String> footerRules) {
		super();
		this.autoNum = autoNum;
		this.fields = fields;
		setHeaderRules(headerRules);
		if (footerRules != null) {
			setFooterRules(footerRules);
		}
	}

	public boolean getIfFooter() {
		return ifFooter;
	}

	public boolean getIfMerge() {
		return ifMerge;
	}

	public String getTitile() {
		return titile;
	}

	private void setTitile(String titile) {
		this.ifMerge = false;
		this.titile = titile;
		this.maxRows = this.maxRows + 1;
	}

	public String[] getHearder() {
		return header;
	}

	private void setHeader(String[] header) {
		this.ifMerge = false;
		this.header = header;
		this.maxRows = this.maxRows + 1;
		this.maxColumns = header.length - 1;
	}

	public int getMaxColumns() {
		return maxColumns;
	}

	public int getMaxRows() {
		return maxRows;
	}

	public HashMap<String, String> getHeaderRules() {
		return headerRules;
	}

	private void setHeaderRules(HashMap<String, String> headerRules) {
		this.headerRules = headerRules;
		// 解析rules，获取最大行和最大列
		Iterator<Entry<String, String>> entries = headerRules.entrySet().iterator();
		int row = 0;
		int col = 0;
		while (entries.hasNext()) {
			Entry<String, String> entry = entries.next();
			String key = entry.getKey();
			Object[] range = coverRange(key);
			int a = (int) range[1];
			int b = POIConstant.cellRefNums.get(range[3]) + 1;
			row = a > row ? a : row;
			col = b > col ? b : col;
		}
		this.maxRows = row;
		this.maxColumns = col;
		this.ifMerge = true;
	}

	/**
	 * 合并单元格转换
	 * 
	 * @param obj
	 * @return
	 */
	private static Object[] coverRange(Object obj) {
		String[] range = ((String) obj).split(",");
		Object[] rangeInt = new Object[4];
		for (int i = 0; i < range.length; i++) {
			if (i < 2) {
				rangeInt[i] = Integer.parseInt(range[i]);
			} else {
				rangeInt[i] = range[i];
			}

		}
		return rangeInt;
	}

	public HashMap<String, String> getFooterRules() {
		return footerRules;
	}

	private void setFooterRules(HashMap<String, String> footerRules) {
		this.ifFooter = true;
		this.footerRules = footerRules;
	}

	public boolean getAutoNum() {
		return autoNum;
	}

	public Object[][] getFields() {
		return fields;
	}

}
